<template>
  <div class="container">
    <div class="chart">
      <LakerEcharts ref="wchart" :option="option" width="100%" height="100%" @chartOnclick="chartOnclick" />
    </div>
  </div>
  <hd-reward ref="hdDialogRef" v-if="dialog.hdDialog" @closed="dialog.hdDialog = false"></hd-reward>
</template>
<script>
import LakerEcharts from '@comp/lakerEcharts/index.vue'
import dprevent from '@/api/dprevent'
import HdReward from './HdReward.vue'
export default {
  name: 'DpRightItem2',
  components: {
    LakerEcharts,
    HdReward
  },
  data() {
    return {
      dialog: {
        hdDialog: false
      },
      option: {
        grid: {
          top: '40',
          left: '40',
          right: '10',
          bottom: '30'
        },
        tooltip: {
          trigger: 'axis'
        },
        xAxis: {
          type: 'category',
          data: ['公司领导', '安环部', '生技部', '运行分厂', '外委单位']
        },
        yAxis: {
          type: 'value',
          name: '单位：元'
        },
        series: [
          {
            name: '发现',
            type: 'bar',
            barWidth: 15,
            stack: 'total',
            label: {
              show: true,
              position: 'top',
              color: '#ccc'
            },
            data: [165, 187, 162, 162, 162]
          },
          {
            name: '整改',
            type: 'bar',
            barWidth: 15,
            stack: 'total',
            label: {
              show: true,
              position: 'top',
              color: '#ccc'
            },
            data: [165, 187, 162, 162, 162]
          },
          {
            name: '验收',
            type: 'bar',
            barWidth: 15,
            stack: 'total',
            label: {
              show: true,
              position: 'top',
              color: '#ccc'
            },
            data: [165, 187, 162, 162, 162]
          }
        ]
      }
    }
  },
  mounted() {
    this.getApiData()
    setInterval(() => {
      this.getApiData()
    }, 5000)
  },
  methods: {
    getApiData() {
      dprevent.dprightapi2().then((res) => {
        if (res.code === 'success') {
          if (res.result) {
            let data = res.result
            this.option.xAxis.data = data.map((item) => item.name)
            this.option.series[0].data = data.map((item) => item.fvalue)
            this.option.series[1].data = data.map((item) => item.zvalue)
            this.option.series[2].data = data.map((item) => item.yvalue)
          }
        }
      })
    },
    chartOnclick(params) {
      this.dialog.hdDialog = true
      this.$nextTick(() => {
        this.$refs.hdDialogRef.open('show').setData(params)
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.container {
  display: flex;
  height: 100%;
  justify-content: right;
  .chart {
    width: 100%;
    height: 100%;
  }
  .title {
    width: 50%;
    display: flex;
    flex-direction: column;
    font-size: 14px;
    align-items: center;
    justify-content: center;
    span {
      font-size: 16px;
      padding: 5px 10px;
    }
  }
}
</style>
